var isLoading = false;

$('document').ready(function() {
    isLoading = true;
    var i = 0;
    $('.post_list div.row').each(function() {
        $(this).delay(200*i).fadeIn(400);
        i++;
    });
    
    $('#post_more').fadeIn(400,function(){
        isLoading = false;
    });
    
    var range = 50;	//距下边界多少长度就进行刷新/单位px
    $(window).mousewheel(function () {    	
    	var winH = $(window).height();	//可视窗口高度
        var pageH = $(document).height(); //页面总高度 
        var scrollT = $(window).scrollTop(); //滚动条top 
        var remainHeight = pageH-winH-scrollT;	//剩余高度
        if ( remainHeight<=range && !isLoading ){ 
            get_more_post();
        }
    });    
    $(window).scroll(function () {    	
    	var winH = $(window).height();	//可视窗口高度
        var pageH = $(document).height(); //页面总高度 
        var scrollT = $(window).scrollTop(); //滚动条top 
        var remainHeight = pageH-winH-scrollT;	//剩余高度
        if ( remainHeight<=range && !isLoading ){ 
            get_more_post();
        }
    });
    
    $('#morebtn').click(function() {
        if( !isLoading ) {
            get_more_post();
        }
        return false;
    });
});

function get_more_post() {
	isLoading = true;        
	$('#morebtn').html("正在载入中...");        
	$.post("post/json_post_list", {
		last_id: parseInt($('#load_count').val())
	}, function(data, textStatus) {
            if(textStatus=="timeout"){  
                $("#morebtn").html("加载超时，点此重试");
                isLoading = false;
            }
            else {
                if(data.code < 0) {                            
                        $('#morebtn').html("已经没有更多的帖子了");
                        $('#post_more').fadeIn(500);
                }
                else if(data.code == 1) {
                        generate_more_post(data.results);
                        $('#morebtn').html("点击此处加载更多..."); 
                }
            }
	}, "json");
	return false;
}

function generate_more_post(post_list) {
	$.get('assets/templates/post_list_single_post.html', function(data) {
                var i = 0;
                $('#post_more').fadeOut(500);
		for(id in post_list) {
			var view = {
                post_id: post_list[id].post_id,
                post_like: post_list[id].post_like,
                repost_num: post_list[id].repost_num,
                click_num: post_list[id].click_num,
                post_title: post_list[id].post_title,
                post_topic: post_list[id].post_topic,
                post_topic_value: post_list[id].post_topic_value,
                post_gender: post_list[id].post_gender,
                post_birth: post_list[id].post_birth,
                post_constel: post_list[id].post_constel,
                post_residence: post_list[id].post_residence,
                post_time: post_list[id].post_time,
                user_id: post_list[id].user_id,
                nickname: post_list[id].nickname				
            };
            var output = Mustache.render(data, view);
            $('#post_more').before(output);
            $("#post_"+post_list[id].post_id).delay(200*i).fadeIn(400);
            i++;
		}
        $('#load_count').val(post_list[post_list.length-1].post_id);
        $('#post_more').fadeIn(400,function(){
            isLoading = false;
        });
	});
}



